﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WorkTimeApplication
{
    public partial class ConfigurationForm : Form
    {
        private bool m_isAnyChanges;

        public ConfigurationForm()
        {
            InitializeComponent();
        }

        private void ConfigurationForm_Load(object sender, EventArgs e)
        {
            RefreshGUI();
            m_isAnyChanges = false;
        }

        private void buttonBrowsePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                textBoxDBPath.Text = fbd.SelectedPath;
            }
            m_isAnyChanges = true;
        }

        protected void RefreshGUI()
        {
            textBoxDBFileName.Text                  = Properties.Settings.Default.DBFileName;
            textBoxDBPath.Text                      = Properties.Settings.Default.DBDirectory;
            numericUpDownDBRefresh.Text             = Properties.Settings.Default.RefreshDBInSeconds.ToString();
            numericUpDownOvertimeThresold.Text      = Properties.Settings.Default.OvertimeThreshold.ToString();
            checkBoxStartWithWindows.Checked        = Properties.Settings.Default.StartWithWindows;
        }

        protected void SaveSettings()
        {
            Properties.Settings.Default.DBFileName          = textBoxDBFileName.Text;
            Properties.Settings.Default.DBDirectory         = textBoxDBPath.Text;
            Properties.Settings.Default.RefreshDBInSeconds  = (uint)numericUpDownDBRefresh.Value;
            Properties.Settings.Default.OvertimeThreshold   = (uint)numericUpDownOvertimeThresold.Value;
            Properties.Settings.Default.StartWithWindows    = checkBoxStartWithWindows.Checked;

            Startup = Properties.Settings.Default.StartWithWindows;

            pl.OC.DB.DBHandler.getInstance().OvertimeThreshold  = (int)numericUpDownOvertimeThresold.Value;
            pl.OC.DB.DBHandler.getInstance().DBFile             = new System.IO.FileInfo(Properties.Settings.Default.DBDirectory + "\\" + Properties.Settings.Default.DBFileName);

            Properties.Settings.Default.Save();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (m_isAnyChanges)
            {
                DialogResult dr = MessageBox.Show("Click Yes to save settings, No to discard or Cancel to take no action", "Save settings?", MessageBoxButtons.YesNoCancel);

                switch (dr)
                {
                    case DialogResult.Cancel:
                    {
                        e.Cancel = true;
                        return;
                    }

                    case DialogResult.Yes:
                    {
                        SaveSettings();
                        break;
                    }

                    case DialogResult.No:
                    {
                        break;
                    }
                }
            }

            base.OnClosing(e);
        }

        private void textBoxDBFileName_TextChanged(object sender, EventArgs e)
        {
            m_isAnyChanges = true;
        }

        private void numericUpDownDBRefresh_ValueChanged(object sender, EventArgs e)
        {
            m_isAnyChanges = true;
        }

        private void numericUpDownOvertimeThresold_ValueChanged(object sender, EventArgs e)
        {
            m_isAnyChanges = true;
        }

        private void checkBoxStartWithWindows_CheckedChanged(object sender, EventArgs e)
        {
            m_isAnyChanges = true;
        }

        /// <summary>
        /// Gets or Sets if the Program will Run on Startup.
        /// </summary>
        private bool Startup
        {
            get
            {
                return checkBoxStartWithWindows.Checked;
            }
            set
            {
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (value == true)
                {
                    key.SetValue(Application.ProductName, Application.ExecutablePath.ToString());
                } 
                else
                {
                    key.DeleteValue(Application.ProductName, false);
                }
            }
        }

        private void numericUpDownDBRefresh_KeyDown(object sender, KeyEventArgs e)
        {
            m_isAnyChanges = true;
        }

        private void numericUpDownOvertimeThresold_KeyDown(object sender, KeyEventArgs e)
        {
            m_isAnyChanges = true;
        }

        private void buttonDBFileNameBrowse_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "XML (*.xml)|*.xml";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                textBoxDBFileName.Text  = sfd.FileName.Substring(sfd.FileName.LastIndexOf("\\") + 1, sfd.FileName.Length - sfd.FileName.LastIndexOf("\\") - 1);
                textBoxDBPath.Text      = sfd.FileName.Substring(0, sfd.FileName.LastIndexOf("\\"));
            }
        }
    }
}